Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

Note:

Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

Solution:

  1. public class Solution {
  2. public int missingNumber(int[] nums) {
  3. int sum = 0, n = nums.length;
  4. for (int i = 0; i < n; i++)
  5. sum += nums[i];
  6. return n * (1 + n) / 2 - sum;
  7. }
  8. }